package bigraph.figure;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

public class PlaceFigure extends Figure {
	
	public static final int PLACE_FIGURE_DEFWIDTH = 150;
	public static final int PLACE_FIGURE_DEFHEIGHT = 150;
	
	private Label placeLabel = new Label();
	private XYLayout layout;
	Figure container;
	Shape shape;
	
	public PlaceFigure() {
		container = new Figure();
		container.setLayoutManager(new XYLayout());
		layout = new XYLayout();
        setLayoutManager(layout);
        add(container);
        placeLabel.setForegroundColor(ColorConstants.gray);
		placeLabel.setBounds(new Rectangle(10,0,50,20));
		placeLabel.setLabelAlignment(PositionConstants.LEFT);
		container.add(placeLabel,-1);
		
		shape = new Ellipse();
		shape.setBounds(container.getBounds());
		container.add(shape);
//		shape.setCornerDimensions(new Dimension(20,20));
		shape.setLineStyle(SWT.LINE_SOLID);
		shape.setLineWidth(2);
		shape.setFill(true);
		shape.setAlpha(40);
		container.setOpaque(false);
		setOpaque(false);
   }
	
    public void setName(String text) {
        placeLabel.setText(text);
   }

    public void setLayout(Rectangle rect) {
		if(getParent()==null) {
			this.setBounds(rect);
			container.setBounds(rect);
			shape.setBounds(getShapeSize(rect));
		}
		else {
			Point point = getParent().getBounds().getLocation();
			Rectangle childRec = new Rectangle(point.x + rect.x, point.y + rect.y, rect.width, rect.height);
			this.setBounds(childRec);
			container.setBounds(childRec);
			shape.setBounds(getShapeSize(childRec));
		}
   }
    
    public void setColor(Color color) {
    	shape.setBackgroundColor(color);
    }
    
    @Override
    public void add(IFigure figure, Object constraint, int index) {
    	// TODO Auto-generated method stub
    	super.add(figure, constraint, -1);
    }
    
    private Rectangle getShapeSize(Rectangle rect) {
    	Rectangle newSize = new Rectangle(rect.x,rect.y+20,rect.width,rect.height-20);
    	return newSize;
    }

}
